﻿
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WcsZz.Extend;
using wms_wpf.Apiservice;
using wms_wpf.Dto;
using wms_wpf.InPut;
using wms_wpf.Views;

namespace wms_wpf.ViewModels
{
   public class BoxConfigViewModel:BindableBase
    {
        private BoxConfigView View { get; set; }
        private  IBaseService service {  get; set; }
        public BoxConfigViewModel(BoxConfigView view) 
         {
            this.View = view;
            BoxDtos = new List<BoxDto>();
            service = new BaseService();
            InPut = new InPut_BaseService6();
            InPut_BaseService2=new InPut_BaseService2();
            InPut.PageIndex = 1;
            InPut.PageSize = 20;
            GetList_BoxsCommand = new DelegateCommand(GetList_Boxs);
            PageUpdatedCmd= new DelegateCommand(GetList_Boxs);
            GetList_Boxs();
            EventAggregatorHelper.EventAggregator.ResgiterMessage(arg =>
            {
                if (!string.IsNullOrEmpty(arg.Message))
                {
                    string[] parts=arg.Message.Split('-');
                    UpdateBox(parts[0], parts[1]);
                }
             

            }, "Dialog3");
        }

        private long count;
        public long Count
        {
            get { return count; }
            set { count = value; RaisePropertyChanged(); }
        }
        private long pagecount;
        public long PageCount
        {
            get { return pagecount; }
            set { pagecount = value; RaisePropertyChanged(); }
        }
        private InPut_BaseService6 inPut;
        public InPut_BaseService6 InPut
        {
            get { return inPut; }
            set { inPut = value;  RaisePropertyChanged(); }
        }
        private List< BoxDto> boxDtos;
        public List<BoxDto> BoxDtos
        {
            get { return boxDtos; }
            set { boxDtos = value;RaisePropertyChanged(); }
        }
        public DelegateCommand GetList_BoxsCommand { get;set; }
        public DelegateCommand PageUpdatedCmd { get; set; }
        public DelegateCommand UpdateBoxCommand { get; set; }
        private bool isrunning;
        public bool Isruning
        {
            get { return isrunning; }
            set { isrunning = value; RaisePropertyChanged(); }
        }

        private Visibility visibility = Visibility.Hidden;

        public Visibility Visibility
        {
            get { return visibility; }
            set { visibility = value; RaisePropertyChanged(); }
        }
        private InPut_BaseService2 inPut_BaseService2;
        public InPut_BaseService2 InPut_BaseService2
        {
            get { return inPut_BaseService2; }
            set { inPut_BaseService2 = value;RaisePropertyChanged(); }

        }
        private async void GetList_Boxs()
        {
            Tuple<List<BoxDto>, long?, long?> tuple =await service.GetList_Boxs(InPut);
            if (tuple == null)
            {
                return;
            }
            BoxDtos = tuple.Item1;
            Count = (long)tuple.Item2;
            PageCount = (long)tuple.Item3;
        }
        private async void UpdateBox(string BoxGridQuantity,string BoxPositionQuantity)
        {
            InPut_BaseService2.BoxGridQuantity = BoxGridQuantity;
            InPut_BaseService2.BoxPositionQuantity = BoxPositionQuantity;
            if (View.datagrid.SelectedItems.Count == 0)
            {
                return;
            }
            foreach (BoxDto item in View.datagrid.SelectedItems)
            {
                InPut_BaseService2.BoxCode.Add(item.BoxCode);

            }
            await service.Update_Box(InPut_BaseService2);




        }
    }
}
